package com.example.twotwo_project;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class Board_Arm extends ActionBarActivity {
	Board_DB db;
	ListView lv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_breast);
		
		lv = (ListView)findViewById(R.id.listView1);
		// 뒤로가기 버튼
		getActionBar().setDisplayHomeAsUpEnabled(true);
	}
	
	// 다시 List 가 호출되었을 때, 발동하는 메서드
	@Override
	protected void onResume() {
		super.onResume();
		db = new Board_DB(this, "note", null, 1);  // 처음에 "note"라는 DB가 없는데 getReadbaleDatabase()로 부르면 NoteDB.java에서
		db.getReadableDatabase();                 //  onCreate() 메서드가 돌면서 새로운 "note"라는 DB가 만들어진다. 
		
		View v = null;
		search(v);
	}
	
	// 노트를 검색할 때,
	public void search(View v){
		// 찾는 문자를 가져온다.
		EditText et = (EditText)findViewById(R.id.editFind);
		Log.i("s", " 검색 ");
		ArrayList<HashMap<String, String>> al = db.select(et);
		SimpleAdapter adapter = new SimpleAdapter(this, al, android.R.layout.simple_list_item_2, // ArrayList 는 데이터를 android.R.layout.simple_list_item_2 형태로 뿌릴 것이다.	
				new String[]{"subject", "_id"}, new int[]{ android.R.id.text1, android.R.id.text2});
		
		ListView lv = (ListView)findViewById(R.id.listView1);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				
				Intent intent = new Intent(getApplicationContext(), Board_ViewAc.class);
				TextView tv = (TextView)findViewById(android.R.id.text2);
				intent.putExtra("id", tv.getText().toString());
				startActivity(intent);
				
			}
		});		
	}
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
			
		

		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(android.view.MenuItem item) {

    	switch(item.getItemId()){
		case android.R.id.home:
			finish();
			return true;
		}
    	
    	return super.onOptionsItemSelected(item);
	}
}
